﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.TextEditor;
using System.IO;
using System.Globalization;
using System.Diagnostics;
using System.Configuration;

namespace CookEdit
{
    public struct PlanUnit
    {
        public string datetime;
        public string directory;
    }
    public partial class MakePlan : Form
    {
        private ListViewItem sitem;
        
        public MakePlan()
        {
            InitializeComponent();
            CreateViewItem();
        }
        /// <summary>
 
        
        private void CreateViewItem()
        {
            #region 增加Item的标题，共有三个列
            //1、创建标题，共三列
            listView1.Columns.Add("Time");
            listView1.Columns.Add("Directory");
            #endregion
            listView1.BeginUpdate();
            //2、定义一个ListViewItem，在View.Details模式下，有点像第一列中一个值
            ListViewItem lvItem;
            //3、定义ListViewSubItem，在View.Details模式下，有点像第二列中一个值
            ListViewItem.ListViewSubItem lvSubItem;

            StreamReader sr = new StreamReader(".\\Plan\\plan.tr");
            string msg,time,temp;
            DateTime dt = DateTime.Now;
            time = dt.ToString("yyyyMMddHHmm",DateTimeFormatInfo.InvariantInfo);
            while(!sr.EndOfStream)
            {
                msg = sr.ReadLine();
                temp = msg.Substring(0, 12);

                if ( string.Compare(time,temp) < 0)
                {
                    lvItem = new ListViewItem();
                    lvItem.Text = temp;
                    listView1.Items.Add(lvItem);
                    lvSubItem = new ListViewItem.ListViewSubItem();
                    lvSubItem.Text = msg.Substring(13, msg.Length - 13);
                    lvItem.SubItems.Add(lvSubItem);
                }

            }
            sr.Close();
            listView1.EndUpdate();
            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count != 0)
            {
                foreach (ListViewItem item in this.listView1.SelectedItems)
                {
                    sitem = item;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddAndModify add = new AddAndModify();
            if (DialogResult.OK == add.ShowFor())
            {   
                listView1.BeginUpdate();

                ListViewItem lvItem = new ListViewItem();
                ListViewItem.ListViewSubItem lvSubItem = new ListViewItem.ListViewSubItem();
                lvItem.Text = add.planUnit.datetime;
                listView1.Items.Add(lvItem);
                lvSubItem.Text = add.planUnit.directory;
                lvItem.SubItems.Add(lvSubItem);

                listView1.EndUpdate();
                listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            AddAndModify add = new AddAndModify();
            if (DialogResult.OK == add.ShowFor(sitem.Text,sitem.SubItems[1].Text))
            {
                listView1.BeginUpdate();
                sitem.Text = add.planUnit.datetime;               
                sitem.SubItems[1].Text = add.planUnit.directory;              
                listView1.EndUpdate();
                listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter(".\\Plan\\plan.tr");
            foreach (ListViewItem item in listView1.Items)
            {
                sw.WriteLine(item.Text + "=" + item.SubItems[1].Text);
            }
            sw.Close();
            string s = System.Environment.CurrentDirectory;
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo();
            p.StartInfo.FileName = ConfigurationManager.AppSettings["PPATH"]; ;
            p.StartInfo.Arguments = " -f " + s + "\\Plan\\plan.tr -p " + s + "\\Plan\\ -r " + s + "\\Plan\\";
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;//让窗体不显示  
            p.Start();
            StreamReader reader = p.StandardOutput;//截取输出流  
            string msg = reader.ReadToEnd();
            p.WaitForExit();
            if (msg == "")
            {
                msg = "Success!\n";
            }
            else
            {
                msg += "Fail!\n";
            }

            MessageBox.Show(msg);          

        }

        private void modifyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddAndModify add = new AddAndModify();
            if (DialogResult.OK == add.ShowFor(sitem.Text, sitem.SubItems[1].Text))
            {
                listView1.BeginUpdate();
                sitem.Text = add.planUnit.datetime;
                sitem.SubItems[1].Text = add.planUnit.directory;
                listView1.EndUpdate();
                listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("Are you sure to delete it?", "Confirm Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                listView1.BeginUpdate();
                listView1.Items.Remove(sitem);
                listView1.EndUpdate();
                listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
        }

    }
}
